<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String basePath = request.getContextPath();
  Object email = request.getSession().getAttribute("email");
  Object name = request.getSession().getAttribute("name");
  Object alias = name;
  if (alias == null) {
    alias = email;
  }
%>
<!DOCTYPE HTML>
<html>
<head>
  <title>写资讯</title>
  <jsp:include page="../common/common-css.jsp"></jsp:include>
  <jsp:include page="../common/common-js.jsp"></jsp:include>

  <link href="<%=basePath%>/plugin/edit/bootstrap-wysihtml5.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="<%=basePath%>/plugin/edit/wysihtml5-0.3.0.js"></script>
  <script type="text/javascript" src="<%=basePath%>/plugin/edit/bootstrap-wysihtml5.js"></script>

  <style>
    input {
      border: none;
    }

    ul {
      margin: 0px;
      padding: 0px;
    }

    .badgeBgColor {
      background-color: blueviolet;
    }
  </style>
</head>
<body style="background-color: #f5f5f5">
<jsp:include page="../../head.jsp"></jsp:include>
<div class="container-fluid" style="padding: 0px">
  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-3"></div>
  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-6"
       style="background-color: #ffffff;margin: 10px 0px;padding-bottom: 100px">
    <table style="width: 100%">
      <tr>
        <th align="center" style="padding: 10px 0px">
          <h3 style="color: #cccccc">
            <input type="text" id="title" style="border: none;width: 100%;padding: 5px" maxlength="60"
                   placeholder="在这里输入标题">
          </h3>
        </th>
      </tr>
      <tr>
        <td style="padding: 10px;">
          <blockquote>
            <input type="text" id="subTitle" style="border: none;width: 100%;padding: 5px" maxlength="60"
                   placeholder="在这里输入副标题">
          </blockquote>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="padding: 10px;color: #999999">
          <small><%=alias%>&nbsp;&nbsp;&nbsp;阅读&nbsp;0</small>
        </td>
      </tr>
    </table>
    <hr>
    <p class="storyTags">
      <a href="javascript:toAddTag()" style="margin: 0px 5px"><span class="label label-danger">添加标签</span></a>
      <a href="javascript:chooseTag(0)" style="margin: 0px 5px;">
        <span class="badge badgeBgColor" id="tag0">未分类</span></a>
    </p>
    <hr>
    <textarea id="content" class="textarea"
              placeholder="从这里开始写正文" rows="15"
              style="resize: none;width: 100%;padding: 10px;border: none">
    </textarea>
    <hr style="width: 100%;margin: 5px 0px">
    <div style="background-color: #ffffff;" class="chooseImg">
      <table style="width: 100%;">
        <tr>
          <td style="padding: 10px 8px;color: darkgrey">
            <i class="icon-picture icon-small" id="imgPath">&nbsp;&nbsp;上传封面</i>
          </td>
          <td align="right" style="padding-right: 15px;width: 20px;color: darkgray">
            <i class="icon-angle-right"></i>
          </td>
        </tr>
      </table>
    </div>
    <input type="file" onchange="uploadFile(this);" name="filePath" id="filePath" style="display: none">

    <hr style="width: 100%;margin: 5px 0px">
    <div class="btn-group" role="group" aria-label="..." style="margin-top: 20px">
      <button onclick="save();" type="button" class="btn btn-default-color">发&nbsp;布</button>
      <button onclick="preview();" type="button" class="btn btn-default">预&nbsp;览</button>
      <button type="button" class="btn btn-default" onclick="history.go(-1)">取&nbsp;消</button>
    </div>
  </div>
</div>
<form id="form" action="<%=basePath%>/previewStory" method="post" style="display: none">
  <input name="name" class="name">
  <input name="title" class="title">
  <input name="subTitle" class="subTitle">
  <input name="content" class="content">
  <input name="logo" class="logo">
  <input name="chooseTag" class="chooseTag">
</form>
<input type="hidden" id="img">
<input type="hidden" id="chooseTag" value="0">
<!-- Modal -->
<div class="modal fade bs-example-modal-sm" id="tagModal" role="dialog" style="margin-top: 10%">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header" align="center">
        添加标签
      </div>
      <div class="modal-body">
        <input type="text" maxlength="8" style="width: 100%;padding: 5px" id="tagName" placeholder="输入标签名">
      </div>
      <div class="modal-footer">
        <button class="btn btn-default-color" onclick="addTag();">添&nbsp;&nbsp;加</button>
      </div>
    </div>
  </div>
</div>
<jsp:include page="../common/common-html.jsp"></jsp:include>
<jsp:include page="/foot.jsp"></jsp:include>
</body>
<script language="JavaScript" type="text/javascript">

  $(".textarea").wysihtml5();
  initStyle();
  loadTag();

  // - 上传图片
  function uploadFile(obj) {
    $("#loadModal").modal({
      keyboard: false,
      backdrop: 'static'
    });
    var url = "<%=basePath%>/img/upload";
    $.ajaxFileUpload({
      url: url,
      fileElementId: "filePath",//file标签的id,
      dateType: "text",
      success: function (data, status) {
        // - ajaxFileUpload这儿必须得转换以下
        data = jQuery.parseJSON(jQuery(data).text());
        if (data.code == 200) {
          var temp = data.data;
          $("#img").val(temp.downloadUrl);
          $("#imgPath").text('  ' + temp.downloadUrl);
        } else {
          alerErr(data.msg);
        }
        $("#loadModal").modal('hide');
      },
      error: function (data, status, e) {
        $("#loadModal").modal('hide');
        $("#filePath").replaceWith($("#upload").clone(true));
        alerErr(e);
      }
    });
  }
  // - 触发上传图片
  $(".chooseImg").mousemove(function () {
    $(this).css("cursor", "pointer");
  })
  $(".chooseImg").mouseout(function () {
    $(this).css("cursor", "default");
  })
  $(".chooseImg").click(function () {
    var email = '<%=email%>';
    if (!validateEmail(email)) {
      return;
    }
    $("#filePath").click();
  })

  function validate(title, subTitle, content, img) {
    if (title == '') {
      alerErr("输入标题");
      return false;
    }
    if (subTitle == '') {
      alerErr("输入副标题");
      return false;
    }
    if (content == "") {
      alerErr("输入内容");
      return false;
    }
    if (img == '') {
      alerErr("选择图片");
      return false;
    }
    return true;
  }

  function preview() {
    $(".btn").blur();
    if (!validateEmail('<%=email%>')) {
      return;
    }
    var title = $.trim($("#title").val());
    var subTitle = $.trim($("#subTitle").val());
    var content = $("#content").val();
    var img = $("#img").val();
    var chooseTag = $("#chooseTag").val();
    if (validate(title, subTitle, content, img)) {
      $(".name").val('<%=alias%>');
      $('.title').val(title);
      $('.subTitle').val(subTitle);
      $('.content').val(content);
      $('.logo').val(img);
      $('.chooseTag').val(chooseTag);
      $("#form").submit();
    }
  }

  function chooseTag(tagId) {
    $(".badge").removeClass('badgeBgColor');
    var id = '#tag' + tagId;
    $(id).addClass('badgeBgColor');
    $("#chooseTag").val(tagId);
  }

  function toAddTag() {
    if (!validateEmail('<%=email%>')) {
      return;
    }
    $("#tagModal").modal();
  }


  function addTag() {
    $(".btn").blur();
    var tagName = $.trim($("#tagName").val());
    if (tagName == '') {
      alerErr("输入标签名称!");
      return;
    }
    var url = '<%=basePath%>/tag/add';
    var param = {name: tagName};
    $.post(url, param, function (data, status) {
      $("#tagName").val('');
      $("#tagModal").modal('hide');
      if (data.code == 200) {
        var tag = data.data;
        var temp = '<a style="margin: 0px 5px" href="javascript:chooseTag(' + tag.id + ')"><span id="tag' + tag.id + '" class="badge">' + tag.name + '</span></a>';
        $('.storyTags').append(temp);
      } else {
        aler(data);
      }
    })
  }

  function loadTag() {
    var url = '<%=basePath%>/tag/list';
    $.post(url, function (data, status) {
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          var tagId = 'tag' + data[i].id;
          var temp = '<a style="margin: 0px 5px" href="javascript:chooseTag(' + data[i].id + ')"><span id="' + tagId + '" class="badge">' + data[i].name + '</span></a>';
          $('.storyTags').append(temp);
        }
      }
    })
  }

  function save() {
    $(".btn").blur();
    if (!validateEmail('<%=email%>')) {
      return;
    }
    var title = $.trim($("#title").val());
    var subTitle = $.trim($("#subTitle").val());
    var content = $.trim($("#content").val());
    var img = $("#img").val();
    var chooseTag = $("#chooseTag").val();
    if (!validate(title, subTitle, content, img)) {
      return;
    }
    var param = {
      content: content,
      title: title,
      subTitle: subTitle,
      img: img,
      email: '<%=email%>',
      chooseTag: chooseTag
    };
    var url = "<%=basePath%>/story/save";
    $.post(url, param, function (data, status) {
      if (data.code == 200) {
        $("#content").val('');
        $("#title").val('');
        $("#subTitle").val('');
        $("#img").val('');
        $("#filePath").val('');
        var story = data.data;
        var backUrl = '<%=basePath%>/story/info?uuid=' + story.uuid;
        window.location = '<%=basePath%>/success?backUrl=' + backUrl;
      } else {
        aler(data);
      }
    })
  }


</script>
</html>
